onerror = (msg, url, line) => {
	function shorten(s) {
		if (s.startsWith(location.origin)) s = s.substr(location.origin.length);
		return s;
	}
	if (!url) return;
	axios
		.post("/api/report", {
			href: shorten(location.href),
			msg,
			url: shorten(url),
			line,
		})
		.then((res) => {});
};

axios.interceptors.response.use(
	(res) => {
		const type = res.headers["content-type"];
		if (type && type.indexOf("text/html") >= 0) {
			if (location.pathname.startsWith("wechat")) {
				history.back();
				close();
			} else {
				alert("会话过期");
				top.location.href = "/";
			}
			return;
		}
		return res;
	},
	(error) => {
		const status = error.response.status;
		if (status == 400 || status == 402) {
			alert(error.response.data.msg);
		} else if (status == 401) {
			history.back();
			close();
		} else if (status == 403) {
			alert("权限不足");
		} else if (status == 413) {
			alert("上传文件过大");
		} else if (status == 502) {
			alert("系统正在更新，请稍等片刻");
		} else alert("系统可能正在更新，请稍后再操作");
		return Promise.reject(error);
	}
);
